<!DOCTYPE html>
<html>
	<head>
		<script src="http://code.jquery.com/jquery-latest.js"></script>
		<script type="text/javascript">
			function toggleCheckedPhone(status) {
				$("#wimbledonTour").find ('input[name="phone"]').each( function() {
					$(this).attr("checked",status);
				})
			}

			function toggleCheckedEmail(status) {
				$("#wimbledonTour").find ('input[name="email"]').each( function() {
					$(this).attr("checked",status);
				})
			}
			
			function lineCount(message) {	
				try {
					return((message.match(/[^\n]*\n[^\n]*/gi).length)+1);
				}
				catch(err) {
					return 1;
				}
			}
		</script>
	</head>
	
	<body>
		<h1>Wimbledon</h1>
		<form id="wimbledonTour" name="wimbledonTour">
			<h2>Phone Numbers</h2>
			<input type="checkbox" id="selectallphone" name="selectallphone" onclick="toggleCheckedPhone(this.checked)" /><label for="selectallphone" style="font-weight:bold">Select All</label>
			<input type="checkbox" id="phone1" name="phone" value="07725639652" /><label for="phone1">Dan Mariash</label>
			<input type="checkbox" id="phone2" name="phone" value="07807749908" /><label for="phone2">Peter Weller</label>
			<input type="checkbox" id="phone3" name="phone" value="07825955702" /><label for="phone3">Lewis Pollard</label><br />
			Other (comma seperate): <input type="text" id="phoneOther" name="phoneOther" /><br /><br />
			
			<h2>E-Mail Address</h2>
			<input type="checkbox" id="selectallemail" name="selectallemail" onclick="toggleCheckedEmail(this.checked)" /><label for="selectallemail"  style="font-weight:bold">Select All</label>
			<input type="checkbox" id="email1" name="email" value="dmariash@gmail.com" /><label for="email1">dmariash@gmail.com</label>
			<input type="checkbox" id="email2" name="email" value="danmariash@uk.ibm.com.com" /><label for="email2">danmariash@uk.ibm.com</label><br />
			Other (comma seperated): <input type="text" id="emailOther" name="emailOther" /><br /><br />	
			
			<h2>Visitors</h2>
			<textarea name="message" rows="10" cols="40"></textarea><br /><br />
			
			<!--Server IP: <input type="text" id="serverIP" name="serverIP" /><br />-->
			Password: <input type="password" id="password" name="password" /><br /><br />
			
			<input type="submit" value="Submit" />
		</form><br />
		
		<div id="smsResult"></div>
		<div id="emailResult"></div>
		
		<script>
			// attach a submit handler to the form
			$("#wimbledonTour").submit(function(event) {

				// clear any results
				document.getElementById('smsResult').innerHTML = "";
				document.getElementById('emailResult').innerHTML = "";
			
				// stop form from submitting normally
				event.preventDefault(); 
				
				// increment tour count
				var tourNumber = $.ajax({type: 'POST', url: "./scripts/count.php", async:false}).responseText;
				
					// set form var
					var $form = $( this ),

					// set date vars
					date = new Date(),
					day = date.getDate(),
					month = date.getMonth(),
					year = date.getFullYear(),
					hours = date.getHours(),
					minutes = date.getMinutes(),
					currentTime = "" + hours + ":" + minutes + "",
					currentDate = "" + day + "/" + month + "/" + year + "",
					
					// set phone vars
					smsPhone = new Array(),
					smsPhoneOther = $form.find ('input[name="phoneOther"]').val(),
					smsPhoneOtherSplit = new Array(),
					smsMessage = $form.find ('textarea[name="message"]').val(),
					smsLineCount = lineCount(smsMessage),
					smsText = "Tour number " + tourNumber + ". There are " + smsLineCount + " visitors on their way. Sent at " + currentTime + " on " + currentDate,
					smsPassword = $form.find ('input[name="password"]').val(),
					smsGateway = "http://9.180.162.59:9090/sendsms",
					//smsGateway = "http://" + $form.find ('input[name="serverIP"]').val() + "/sendsms",
					
					// set email vars
					emailAddress = new Array(),
					emailAddressOther = $form.find ('input[name="emailOther"]').val(),
					emailAddressOtherSplit = new Array(),
					emailSubject = "hello",
					emailMessage = $form.find ('textarea[name="message"]').val(),
					emailFrom = "dmariash@gmail.com";
					
					if (smsPhoneOther != "") {smsPhoneOtherSplit = smsPhoneOther.split(',');};			
					if (emailAddressOther != "") {emailAddressOtherSplit = emailAddressOther.split(',')};
					

				// put all checkbox phone numbers in smsPhone array
				for(var i=0; i < document.wimbledonTour.phone.length; i++){
					if(document.wimbledonTour.phone[i].checked){
						smsPhone.push(document.wimbledonTour.phone[i].value);
					}
				}
				
				// put all typed phone numbers in smsPhone array, comma separated
				if (smsPhoneOtherSplit.length > 0){
					for(var i=0; i < smsPhoneOtherSplit.length; i++){
						smsPhone.push(smsPhoneOtherSplit[i]);
					}
				
				}
				// put all email addresses in an array
				for(var i=0; i < document.wimbledonTour.email.length; i++){
					if(document.wimbledonTour.email[i].checked){
						emailAddress.push(document.wimbledonTour.email[i].value);
					}
				}

				// put all typed email address in emailAddress array, comma separated
				if (emailAddressOtherSplit.length > 0){
					for(var i=0; i < emailAddressOtherSplit.length; i++){
						emailAddress.push(emailAddressOtherSplit[i]);
					}
				}
/*
				for(var i=0; i < smsPhone.length; i++){
					alert(smsPhone[i]);
				}
				
				for(var i=0; i < emailAddress.length; i++){
					alert(emailAddress[i]);
				}		
*/
/*				
				for(var i=0; i < smsPhone.length; i++){				
					document.getElementById('smsResult').innerHTML += "SMS successfully sent to <b><i>" + smsPhone[i] + "</i></b>, with the message <b><i>" + smsText + "</i></b><br />";
					$.post("./scripts/writelog.php", { message: currentTime + " " + currentDate + "          SMS successfully sent to " + smsPhone[i] + ", with the message " + smsText + "\n" } );
				}

				for(var i=0; i < emailAddress.length; i++){
					document.getElementById('emailResult').innerHTML += "E-Mail successfully sent to <b><i>" + emailAddress[i] + "</i></b> ,with the message <b><i>" + emailMessage + "</i></b><br />";	
					$.post("./scripts/writelog.php", { message: currentTime + " " + currentDate + "          E-Mail successfully sent to " + emailAddress[i] } );					
				}			
*/
	
				// send sms using get
				for(var i=0; i < smsPhone.length; i++){
					$.get(smsGateway, { phone: smsPhone[i], text: smsText, password: smsPassword } );
					document.getElementById('smsResult').innerHTML += "SMS successfully sent to <b><i>" + smsPhone[i] + "</i></b>, with the message <b><i>" + smsText + "</i></b><br />";
					$.post("./scripts/writelog.php", { message: currentTime + " " + currentDate + "          SMS successfully sent to " + smsPhone[i] + ", with the message " + smsText + "\n" } );
				}
				
				// send email using post
				for(var i=0; i < emailAddress.length; i++){
					$.post("./scripts/sendmail.php", { email: emailAddress[i], subject: emailSubject, message: emailMessage, from: emailFrom } );
					document.getElementById('emailResult').innerHTML += "E-Mail successfully sent to <b><i>" + emailAddress[i] + "</i></b> ,with the message <b><i>" + emailMessage + "</i></b><br />";	
					$.post("./scripts/writelog.php", { message: currentTime + " " + currentDate + "          E-Mail successfully sent to " + emailAddress[i] } );		
				}
				
			});
		</script>
	</body>
</html>